#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#
#
#

"""
#TODO: documentation!
"""

__author__ = 'DR0ID'
__last_change_by__ = '$Author$'
__revision__ = '$Revision$'
__date__ = '$Date$'
__license__ = 'see readme.txt'
__copyright__ = 'Dlacewell, DR0ID (c) 2008'
__url__ = 'http://code.google.com/p/bipy/'
__email__ = 'dr0iddr0id@googlemail.com, DLacewell@googlemail.com'

# make sure the test can find the bipy module
import sys
import unittest
from os.path import split, abspath
BIPY_PATH = split(split(split(abspath(__file__))[0])[0])[0]
if BIPY_PATH not in sys.path:
    sys.path.insert(0, BIPY_PATH)

import bipy
import bipy.scenes

#-----  special state(s) used for testing -----#
class TestScene(bipy.scenes.Scene):
    def __init__(self, engine):
        super(TestScene, self).__init__(engine)
        
        self.entered = False
        self.left = False
        self.frozen = False
        self.unfrozen = False
    
    def on_state_enter(self):
        self.entered = True
    def on_state_leave(self):
        self.left = True
    def on_state_freeze(self):
        self.frozen = True
    def on_state_unfreeze(self):
        self.unfrozen = True
        
class SelfPoppingState(bipy.scenes.Scene):
    def __init__(self, engine):
        super(SelfPoppingState, self).__init__(engine)
        
    def on_state_enter(self):
        self.engine.pop_state()
        


class UTest_Engine(unittest.TestCase):
    def setUp(self):
        self.engine = bipy.engine.EngineClass()
        self.state = TestState()
        self.state2 = TestState()
        self.on_init = False # test 10
        
    def tearDown(self):
        for i in range(10):
            self.engine.pop_state()
            
    def t00_state_count(self):
        self.failIf(self.engine.get_num_scenes(), 'no scene should be present')
        self.engine.push_state(self.state)
        self.failUnless(1 == self.engine.get_num_scenes(), 'engine should have exactly 1 scene')
        self.engine.push_state(self.state)
        self.failUnless(2 == self.engine.get_num_scenes(), 'engine should have exactly 2 scenes')
        self.engine.pop_state()
        self.failUnless(1 == self.engine.get_num_scenes(), 'engine should have exactly 1 scene')
        self.engine.pop_state()
        self.failUnless(0 == self.engine.get_num_scenes(), 'engine should have exactly 0 scenes')
        
    def t01_clear_states(self):
        for i in range(10):
            self.engine.push_state(self.state)
        self.failUnless(10 == self.engine.get_num_scenes(), 'engine should have exactly 9 scenes')
        self.engine.clear_states()
        self.failUnless(0 == self.engine.get_num_scenes(), 'engine should have exactly 0 scenes')
        
    def t02_pop_empty(self):
        try:
            self.engine.pop_state()
        except Exception, e:
            self.fail(e)
            
    def t03_self_popping(self):
        spstate = SelfPoppingState(self.engine)
        self.engine.push_state(spstate)
        self.failUnless(0 == self.engine.get_num_scenes(), 'engine should have exactly 0 scenes')
        
    def t04_scene_access(self):
        self.engine.push_state(self.state)
        self.failUnlessRaises(AttributeError, getattr, self.engine, '__current_state', None)
    
    def t05_comp_install(self):
        comp = bipy.components.Component('testcomp', 10)
        self.engine.add_component(comp)
        self.failUnless(self.engine.has('testcomp'), 'engine must have testcomp attribute')
        
if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(unittest.makeSuite(UTest_Engine))